package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis 初始化监听器
 * spring 容器准备好之后立马执行初始化逻辑
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.err.println("start");
        //查询mongodb中攻略数据
        List<Strategy> strategies = strategyService.listAll();
        for (Strategy list : strategies){
            //如果redis中存在vo对象，则跳过
            if (strategyStatisVORedisService.isStrategyVOExists(list.getId())){
                continue;
            }
            //不存在则初始化vo
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(list,vo);
            vo.setStrategyId(list.getId());
            strategyStatisVORedisService.setStrategyStatisVO(vo);
        }

        System.err.println("end");

    }
}
